代码描述:基于C#的上行回复接口调用示例

接口地址:http://api.yunzhixin.com:11140/txp/pullReply


 starSky

     

 2018-02-27 10:04

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

using System;

using System.Net;

using System.Security.Cryptography;

using System.Text;

using Newtonsoft.Json;

namespace YZX_Demo

{

public class ReplyResponse

{

public string Return_Code { get; set; }

public int Total { get; set; }

public ReplyInfo[] Row { get; set; }

}

public class ReplyInfo

{

public string Mobile { get; set; }

public string ReceiveTime { get; set; }

public string ReturnInfo { get; set; }

}

class Program

{

private const string ServerUrl = "http://api.yunzhixin.com:11140"; //云智信平台地址;

private const string Account = "18600000000";//商户编号

private const string TradeKey = "202cb962ac59075b964b07152d234b70";//商户密钥

private static void Main()

{

//拼接加密原文

var signSource = $"{Account}#{TradeKey}";

var sign = Md5Encrypt(signSource);

//拼接Post参数

var postData = $"account={Account}&sign={sign}";

//执行请求

var response = string.Empty;

try

{

response = HttpPost(url, postData);

}

catch (Exception e)

{

Console.WriteLine($"请求异常:{e}");

}

//处理返回结果

if(!string.IsNullOrEmpty(response))

{

var replyResponse = JsonConvert.DeserializeObject(response);

Console.WriteLine($"返回码:{replyResponse.Return_Code}");

Console.WriteLine($"上行回复数量:{replyResponse.Total}");

if(replyResponse.Row?.Length>0)

{

foreach (var replyInfo in replyResponse.Row)

{

Console.WriteLine($"手机号:{replyInfo.Mobile}");

Console.WriteLine($"接收时间:{replyInfo.ReceiveTime}");

Console.WriteLine($"信息内容:{replyInfo.ReturnInfo}");

}

}

}

Console.ReadKey();

}

/// <summary>

/// Md5加密

/// </summary>

///加密原文

///密文

public static string Md5Encrypt(string input)

{

var md5 = new MD5CryptoServiceProvider();

var buffer = md5.ComputeHash(Encoding.UTF8.GetBytes(input));

var builder = new StringBuilder(32);

foreach (var t in buffer)

{

builder.Append(t.ToString("x").PadLeft(2, '0'));

}

return builder.ToString();

}

///

/// Http Post请求

///

/// 请求地址

/// 参数

/// 编码

/// 请求响应

public static string HttpPost(string url, string data, Encoding enc = null)

{

if (enc == null) enc = Encoding.UTF8;

var postdata = Encoding.UTF8.GetBytes(data);

var webClient = new WebClient

{

Encoding = enc

};

webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

webClient.Headers.Add(HttpRequestHeader.KeepAlive, "False");

var response = webClient.UploadData(url, "POST", postdata); //得到返回字符流

return enc.GetString(response); //解码

}

}

}